import Utils.HttpUtils;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @author: Enid
 * @date: 2025/10/9 15:03
 * @description:
 */
public class WeatherApi {
    // 在main方法外或作为静态变量定义映射表
    private static final Map<String, String> FIELD_NAME_MAP = new HashMap<String, String>() {{
        // City字段映射
        put("cityId", "城市ID");
        put("counname", "国家名称");
        put("ianatimezone", "时区");
        put("name", "城市名称");
        put("pname", "省份名称");
        put("secondaryname", "二级行政区");
        put("timezone", "时区偏移");

        // Condition字段映射
        put("condition", "天气状况");
        put("conditionId", "天气编码");
        put("humidity", "湿度");
        put("icon", "天气图标");
        put("pressure", "气压");
        put("realFeel", "体感温度");
        put("sunRise", "日出时间");
        put("sunSet", "日落时间");
        put("temp", "当前温度");
        put("tips", "天气提示");
        put("updatetime", "更新时间");
        put("uvi", "紫外线指数");
        put("vis", "能见度");
        put("windDegrees", "风向角度");
        put("windDir", "风向");
        put("windLevel", "风力等级");
        put("windSpeed", "风速");
    }};

    public static void main(String[] args) {
        String host = "https://aliv18.data.moji.com";
        String path = "/whapi/json/alicityweather/condition";
        String method = "POST";
        String appcode = "380164e0b882475ead478d7ccc3c77ef";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        Map<String, String> querys = new HashMap<String, String>();
        Map<String, String> bodys = new HashMap<String, String>();
        bodys.put("cityId", "1185");
        bodys.put("token", "50b53ff8dd7d9fa320d3d3ca32cf8ed1");

        try {
            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
            // 假设已添加Jackson依赖
            ObjectMapper mapper = new ObjectMapper();
            JsonNode rootNode = mapper.readTree(EntityUtils.toString(response.getEntity()));
            JsonNode dataNode = rootNode.get("data");

            JsonNode cityNode = dataNode.get("city");
            JsonNode conditionNode = dataNode.get("condition");

// 遍历city对象
            System.out.println("城市信息：");
            displayCondition(cityNode);
            System.out.println("==========================================");
// 遍历condition对象
            System.out.println("天气状况：");
            displayCondition(conditionNode);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private static void displayCondition(JsonNode conditionNode) {
        Iterator<Map.Entry<String, JsonNode>> conditionFields = conditionNode.fields();
        while (conditionFields.hasNext()) {
            Map.Entry<String, JsonNode> field = conditionFields.next();
            String chineseName = FIELD_NAME_MAP.getOrDefault(field.getKey(), field.getKey());
            System.out.println("  " + chineseName + ": " + field.getValue().asText());
        }
    }
}
